package com.sskj.givecoin.v.activity;

import android.content.Intent;
import android.support.constraint.Guideline;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.Gravity;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.sskj.givecoin.R;
import com.sskj.givecoin.common.App;
import com.sskj.givecoin.common.MoneyValueFilter;
import com.sskj.givecoin.common.MyTextWatcher;
import com.sskj.givecoin.common.adapter.BaseAdapter;
import com.sskj.givecoin.common.adapter.ViewHolder;
import com.sskj.givecoin.common.widget.SelectView;
import com.sskj.givecoin.common.widget.ToolBarLayout;
import com.sskj.givecoin.m.bean.CoinEntry;
import com.sskj.givecoin.m.bean.ExchangeCoin;
import com.sskj.givecoin.m.bean.UserInfo;
import com.sskj.givecoin.p.activity.ExchangePresenter;
import com.sskj.givecoin.utils.ClickUtil;
import com.sskj.givecoin.utils.NumberUtil;
import com.sskj.givecoin.utils.ToastUtil;
import com.sskj.givecoin.v.base.BaseActivity;

import java.util.List;

import butterknife.BindView;

/**
 * 兑换
 */
public class ExchangeActivity extends BaseActivity<ExchangePresenter> {
    @BindView(R.id.toolbar)
    ToolBarLayout toolbar;
    @BindView(R.id.guideline13)
    Guideline guideline13;
    @BindView(R.id.select_unit2)
    SelectView selectUnit2;
    @BindView(R.id.select_unit1)
    SelectView selectUnit1;
    @BindView(R.id.exchange_num)
    EditText exchangeNum;
    @BindView(R.id.unit1)
    TextView unit1;
    @BindView(R.id.asset_num)
    TextView assetNum;
    @BindView(R.id.unit2)
    TextView unit2;
    @BindView(R.id.password)
    EditText password;

    @BindView(R.id.submit)
    Button submit;

    private double rate;

    private String coin;
    private String ex_coin;

    private UserInfo userInfo;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_exchange;
    }

    @Override
    public ExchangePresenter getPresenter() {
        return new ExchangePresenter();
    }

    @Override
    protected void initView() {
        toolbar.setLeftButtonOnClickLinster(v -> {
            finish();
        });
        //兑换历史
        toolbar.setRightButtonOnClickLinster(v -> {
            startActivity(new Intent(this, ExchangeHistoryActivity.class));
        });
        exchangeNum.addTextChangedListener(new MyTextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                calcExchange();
            }
        });
        exchangeNum.setFilters(new InputFilter[]{new MoneyValueFilter(4)});
        //兑换
        ClickUtil.click(submit, () -> {
            if (!checkIdent()) {
                return;
            }

            if (userInfo != null) {
                if (TextUtils.isEmpty(userInfo.getTpwd())) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.strLevelTransactionFragmentX279));
                    startActivity(new Intent(mActivity, PayPwdSettingActivity.class));
                    return;
                }
            }

            if (TextUtils.isEmpty(exchangeNum.getText()) || Double.parseDouble(exchangeNum.getText().toString()) == 0) {
                ToastUtil.showShort(getString(R.string.input_exchange_num));
                return;
            }
            if (TextUtils.isEmpty(password.getText())) {
                ToastUtil.showShort(getString(R.string.xmlactivity_pay_pwd_input32));
                return;
            }
            mPresenter.exchange(coin, ex_coin, exchangeNum.getText().toString(), password.getText().toString());
        });
    }

    private void calcExchange() {
        if (!TextUtils.isEmpty(exchangeNum.getText())) {
            Double num = Double.parseDouble(exchangeNum.getText().toString()) * rate;
            assetNum.setText(NumberUtil.keepMax4(num));
        } else {
            assetNum.setText("0");
        }
    }

    @Override
    protected void initData() {
        //获取用户信息
        mPresenter.getUserInfo();
        //获取可兑换币种
        mPresenter.getExchangeCoinList();
    }

    /**
     * 币种的兑换列表
     *
     * @param data
     */
    public void setExchangeCoinData(List<ExchangeCoin> data) {
        if (data!=null&&data.size()>0){
            selectUnit2.setText(data.get(0).getEx_name());
            unit2.setText(data.get(0).getEx_name());
       }
        selectUnit2.bindAdapter(new BaseAdapter<ExchangeCoin>(R.layout.item_spinner_exchange, data, null) {
            @Override
            public void bind(ViewHolder holder, ExchangeCoin item) {
                TextView textView = holder.getView(R.id.text);
                textView.setText(item.getEx_name());
                textView.setGravity(Gravity.CENTER);
                textView.setOnClickListener(v -> {
                    selectUnit2.setText(item.getEx_name());
                    unit2.setText(item.getEx_name());
                    rate = item.getEx_rate();
                    ex_coin = item.getEx_coin();
                    selectUnit2.dismiss();
                });

            }

        });

    }

    /**
     * 可兑换币种
     *
     * @param data
     */
    public void setExchangeAbleList(List<CoinEntry> data) {
        if (data!=null&&data.size()>0){
            selectUnit1.setText(data.get(0).getPname());
            mPresenter.getCoinList(data.get(0).getCode());
        }
        selectUnit1.bindAdapter(new BaseAdapter<CoinEntry>(R.layout.item_spinner_exchange, data, null) {
            @Override
            public void bind(ViewHolder holder, CoinEntry item) {
                TextView textView = holder.getView(R.id.text);
                textView.setText(item.getPname());
                textView.setGravity(Gravity.CENTER);
                textView.setOnClickListener(v -> {
                    selectUnit1.setText(item.getPname());
                    unit1.setText(item.getPname());

                    coin = item.getCode();
                    mPresenter.getCoinList(item.getCode());
                    selectUnit1.dismiss();
                });
            }
        });


    }

    public void submitSuccess() {
        password.getText().clear();
        startActivity(new Intent(this, ExchangeHistoryActivity.class));

    }

    public void updateUserInfo(UserInfo data) {
        this.userInfo = data;
    }
}
